from PySide2.QtWidgets import QComboBox, QWidget, QApplication
from PySide2.QtCore import Qt, Signal
from PySide2.QtGui import QFont, QPalette
from enum import Enum


class ComboBoxStyle(Enum):
    """ComboBox样式枚举"""
    DEFAULT = "default"
    MODERN = "modern"
    ROUNDED = "rounded"
    FLAT = "flat"
    MATERIAL = "material"


class ThemeMode(Enum):
    """主题模式枚举"""
    LIGHT = "light"
    DARK = "dark"


class CustomComboBox(QComboBox):
    """自定义ComboBox控件
    
    支持多种风格和主题模式的ComboBox控件
    """
    
    # 信号
    theme_changed = Signal(str)  # 主题改变信号
    
    def __init__(self, style=ComboBoxStyle.DEFAULT, theme=ThemeMode.LIGHT, parent=None):
        super().__init__(parent)
        self._style = style
        # 处理字符串类型的主题参数
        if isinstance(theme, str):
            self._theme = ThemeMode.DARK if theme.lower() == "dark" else ThemeMode.LIGHT
        else:
            self._theme = theme
        self._init_ui()
        self._apply_style()
    
    def _init_ui(self):
        """初始化UI"""
        # 设置基本属性
        self.setMinimumHeight(32)
        self.setMaximumHeight(40)
        self.setFont(QFont("Arial", 15))
        
        # 连接信号
        self.currentTextChanged.connect(self._on_text_changed)
    
    def _on_text_changed(self, text):
        """文本改变时的处理"""
        pass
    
    def set_style(self, style: ComboBoxStyle):
        """设置样式"""
        self._style = style
        self._apply_style()
    
    def set_theme(self, theme: ThemeMode):
        """设置主题"""
        self._theme = theme
        self._apply_style()
        self.theme_changed.emit(theme.value)
    
    def update_theme(self, theme_name: str):
        """更新主题（接受字符串参数）"""
        if theme_name.lower() == "dark":
            self.set_theme(ThemeMode.DARK)
        else:
            self.set_theme(ThemeMode.LIGHT)
    
    def get_style(self) -> ComboBoxStyle:
        """获取当前样式"""
        return self._style
    
    def get_theme(self) -> ThemeMode:
        """获取当前主题"""
        return self._theme
    
    def _apply_style(self):
        """应用样式"""
        style_sheet = self._get_style_sheet()
        self.setStyleSheet(style_sheet)
    
    def _get_style_sheet(self) -> str:
        """获取样式表"""
        if self._style == ComboBoxStyle.DEFAULT:
            return self._get_default_style()
        elif self._style == ComboBoxStyle.MODERN:
            return self._get_modern_style()
        elif self._style == ComboBoxStyle.ROUNDED:
            return self._get_rounded_style()
        elif self._style == ComboBoxStyle.FLAT:
            return self._get_flat_style()
        elif self._style == ComboBoxStyle.MATERIAL:
            return self._get_material_style()
        else:
            return self._get_default_style()
    
    def _get_default_style(self) -> str:
        """获取默认样式"""
        if self._theme == ThemeMode.LIGHT:
            return """
                QComboBox {
                    background-color: #ffffff;
                    border: 1px solid #cccccc;
                    border-radius: 4px;
                    padding: 6px 12px;
                    color: #333333;
                    font-size: 15px;
                    min-height: 20px;
                }
                
                QComboBox:hover {
                    border-color: #0078d4;
                    background-color: #f8f9fa;
                }
                
                QComboBox:focus {
                    border-color: #0078d4;
                    outline: none;
                }
                
                QComboBox::drop-down {
                    subcontrol-origin: padding;
                    subcontrol-position: top right;
                    width: 20px;
                    border-left: 1px solid #cccccc;
                    background-color: #f8f9fa;
                }
                
                QComboBox::down-arrow {
                    image: url(:/mqss/down_list.svg);
                    width: 12px;
                    height: 12px;
                    margin: 0px;
                    subcontrol-origin: content;
                    subcontrol-position: center;
                }
                
                QComboBox QAbstractItemView {
                    background-color: #ffffff;
                    border: 1px solid #cccccc;
                    selection-background-color: #e3f2fd;
                    selection-color: #333333;
                    outline: none;
                    font-size: 16px;
                    padding: 8px 12px;
                    min-height: 24px;
                }
                
                QComboBox QAbstractItemView::item {
                    padding: 8px 12px;
                    min-height: 24px;
                    border: none;
                }
            """
        else:  # DARK theme
            return """
                QComboBox {
                    background-color: #2d2d2d;
                    border: 1px solid #555555;
                    border-radius: 4px;
                    padding: 6px 12px;
                    color: #ffffff;
                    font-size: 15px;
                    min-height: 20px;
                }
                
                QComboBox:hover {
                    border-color: #4a9eff;
                    background-color: #3a3a3a;
                }
                
                QComboBox:focus {
                    border-color: #4a9eff;
                    outline: none;
                }
                
                QComboBox::drop-down {
                    subcontrol-origin: padding;
                    subcontrol-position: top right;
                    width: 20px;
                    border-left: 1px solid #555555;
                    background-color: #3a3a3a;
                }
                
                QComboBox::down-arrow {
                    image: url(:/mqss/down_list.svg);
                    width: 12px;
                    height: 12px;
                    margin: 0px;
                    subcontrol-origin: content;
                    subcontrol-position: center;
                }
                
                QComboBox QAbstractItemView {
                    background-color: #2d2d2d;
                    border: 1px solid #555555;
                    selection-background-color: #404040;
                    selection-color: #ffffff;
                    outline: none;
                    font-size: 16px;
                    padding: 8px 12px;
                    min-height: 24px;
                }
                
                QComboBox QAbstractItemView::item {
                    padding: 8px 12px;
                    min-height: 24px;
                    border: none;
                }
            """
    
    def _get_modern_style(self) -> str:
        """获取现代样式"""
        if self._theme == ThemeMode.LIGHT:
            return """
                QComboBox {
                    background-color: #ffffff;
                    border: 2px solid #e0e0e0;
                    border-radius: 8px;
                    padding: 8px 16px;
                    color: #2c3e50;
                    font-size: 15px;
                    font-weight: 500;
                    min-height: 24px;
                }
                
                QComboBox:hover {
                    border-color: #3498db;
                    background-color: #f8f9fa;
                }
                
                QComboBox:focus {
                    border-color: #3498db;
                    background-color: #ffffff;
                }
                
                QComboBox::drop-down {
                    subcontrol-origin: padding;
                    subcontrol-position: top right;
                    width: 24px;
                    border: none;
                    background-color: transparent;
                }
                
                QComboBox::down-arrow {
                    image: url(:/mqss/down_list.svg);
                    width: 14px;
                    height: 14px;
                    margin: 0px;
                    subcontrol-origin: content;
                    subcontrol-position: center;
                }
                
                QComboBox QAbstractItemView {
                    background-color: #ffffff;
                    border: 2px solid #3498db;
                    border-radius: 8px;
                    selection-background-color: #3498db;
                    selection-color: #ffffff;
                    outline: none;
                    padding: 4px;
                    font-size: 16px;
                    min-height: 28px;
                }
                
                QComboBox QAbstractItemView::item {
                    padding: 10px 16px;
                    min-height: 28px;
                    border: none;
                    border-radius: 4px;
                }
            """
        else:  # DARK theme
            return """
                QComboBox {
                    background-color: #34495e;
                    border: 2px solid #5a6c7d;
                    border-radius: 8px;
                    padding: 8px 16px;
                    color: #ecf0f1;
                    font-size: 15px;
                    font-weight: 500;
                    min-height: 24px;
                }
                
                QComboBox:hover {
                    border-color: #52c4ff;
                    background-color: #3d566e;
                }
                
                QComboBox:focus {
                    border-color: #52c4ff;
                    background-color: #34495e;
                }
                
                QComboBox::drop-down {
                    subcontrol-origin: padding;
                    subcontrol-position: top right;
                    width: 24px;
                    border: none;
                    background-color: transparent;
                }
                
                QComboBox::down-arrow {
                    image: url(:/mqss/down_list.svg);
                    width: 14px;
                    height: 14px;
                    margin: 0px;
                    subcontrol-origin: content;
                    subcontrol-position: center;
                }
                
                QComboBox QAbstractItemView {
                    background-color: #34495e;
                    border: 2px solid #52c4ff;
                    border-radius: 8px;
                    selection-background-color: #52c4ff;
                    selection-color: #ffffff;
                    outline: none;
                    padding: 4px;
                    font-size: 16px;
                    min-height: 28px;
                }
                
                QComboBox QAbstractItemView::item {
                    padding: 10px 16px;
                    min-height: 28px;
                    border: none;
                    border-radius: 4px;
                }
            """
    
    def _get_rounded_style(self) -> str:
        """获取圆角样式"""
        if self._theme == ThemeMode.LIGHT:
            return """
                QComboBox {
                    background-color: #ffffff;
                    border: 1px solid #ddd;
                    border-radius: 20px;
                    padding: 8px 20px;
                    color: #333;
                    font-size: 15px;
                    min-height: 20px;
                }
                
                QComboBox:hover {
                    border-color: #007acc;
                    background-color: #f0f8ff;
                }
                
                QComboBox:focus {
                    border-color: #007acc;
                    background-color: #ffffff;
                }
                
                QComboBox::drop-down {
                    subcontrol-origin: padding;
                    subcontrol-position: top right;
                    width: 30px;
                    border: none;
                    background-color: transparent;
                    border-radius: 20px;
                }
                
                QComboBox::down-arrow {
                    image: url(:/mqss/down_list.svg);
                    width: 12px;
                    height: 12px;
                    margin: 0px;
                    subcontrol-origin: content;
                    subcontrol-position: center;
                }
                
                QComboBox QAbstractItemView {
                    background-color: #ffffff;
                    border: 1px solid #007acc;
                    border-radius: 12px;
                    selection-background-color: #007acc;
                    selection-color: #ffffff;
                    outline: none;
                    font-size: 16px;
                    padding: 6px;
                    min-height: 26px;
                }
                
                QComboBox QAbstractItemView::item {
                    padding: 9px 20px;
                    min-height: 26px;
                    border: none;
                    border-radius: 8px;
                }
            """
        else:  # DARK theme
            return """
                QComboBox {
                    background-color: #2c2c2c;
                    border: 1px solid #555;
                    border-radius: 20px;
                    padding: 8px 20px;
                    color: #fff;
                    font-size: 15px;
                    min-height: 20px;
                }
                
                QComboBox:hover {
                    border-color: #4da6ff;
                    background-color: #3a3a3a;
                }
                
                QComboBox:focus {
                    border-color: #4da6ff;
                    background-color: #2c2c2c;
                }
                
                QComboBox::drop-down {
                    subcontrol-origin: padding;
                    subcontrol-position: top right;
                    width: 30px;
                    border: none;
                    background-color: transparent;
                    border-radius: 20px;
                }
                
                QComboBox::down-arrow {
                    image: url(:/mqss/down_list.svg);
                    width: 12px;
                    height: 12px;
                    margin: 0px;
                    subcontrol-origin: content;
                    subcontrol-position: center;
                }
                
                QComboBox QAbstractItemView {
                    background-color: #2c2c2c;
                    border: 1px solid #4da6ff;
                    border-radius: 12px;
                    selection-background-color: #4da6ff;
                    selection-color: #ffffff;
                    outline: none;
                    font-size: 16px;
                    padding: 6px;
                    min-height: 26px;
                }
                
                QComboBox QAbstractItemView::item {
                    padding: 9px 20px;
                    min-height: 26px;
                    border: none;
                    border-radius: 8px;
                }
            """
    
    def _get_flat_style(self) -> str:
        """获取扁平样式"""
        if self._theme == ThemeMode.LIGHT:
            return """
                QComboBox {
                    background-color: #f5f5f5;
                    border: none;
                    border-bottom: 2px solid #e0e0e0;
                    border-radius: 0px;
                    padding: 8px 12px;
                    color: #333;
                    font-size: 15px;
                    min-height: 20px;
                }
                
                QComboBox:hover {
                    border-bottom-color: #2196f3;
                    background-color: #eeeeee;
                }
                
                QComboBox:focus {
                    border-bottom-color: #2196f3;
                    background-color: #f5f5f5;
                }
                
                QComboBox::drop-down {
                    subcontrol-origin: padding;
                    subcontrol-position: top right;
                    width: 20px;
                    border: none;
                    background-color: transparent;
                }
                
                QComboBox::down-arrow {
                    image: url(:/mqss/down_list.svg);
                    width: 12px;
                    height: 12px;
                    margin: 0px;
                    subcontrol-origin: content;
                    subcontrol-position: center;
                }
                
                QComboBox QAbstractItemView {
                    background-color: #ffffff;
                    border: 1px solid #e0e0e0;
                    selection-background-color: #2196f3;
                    selection-color: #ffffff;
                    outline: none;
                    font-size: 16px;
                    padding: 4px;
                    min-height: 24px;
                }
                
                QComboBox QAbstractItemView::item {
                    padding: 8px 12px;
                    min-height: 24px;
                    border: none;
                }
            """
        else:  # DARK theme
            return """
                QComboBox {
                    background-color: #424242;
                    border: none;
                    border-bottom: 2px solid #616161;
                    border-radius: 0px;
                    padding: 8px 12px;
                    color: #fff;
                    font-size: 15px;
                    min-height: 20px;
                }
                
                QComboBox:hover {
                    border-bottom-color: #64b5f6;
                    background-color: #4a4a4a;
                }
                
                QComboBox:focus {
                    border-bottom-color: #64b5f6;
                    background-color: #424242;
                }
                
                QComboBox::drop-down {
                    subcontrol-origin: padding;
                    subcontrol-position: top right;
                    width: 20px;
                    border: none;
                    background-color: transparent;
                }
                
                QComboBox::down-arrow {
                    image: url(:/mqss/down_list.svg);
                    width: 12px;
                    height: 12px;
                    margin: 0px;
                    subcontrol-origin: content;
                    subcontrol-position: center;
                }
                
                QComboBox QAbstractItemView {
                    background-color: #424242;
                    border: 1px solid #616161;
                    selection-background-color: #64b5f6;
                    selection-color: #ffffff;
                    outline: none;
                    font-size: 16px;
                    padding: 4px;
                    min-height: 24px;
                }
                
                QComboBox QAbstractItemView::item {
                    padding: 8px 12px;
                    min-height: 24px;
                    border: none;
                }
            """
    
    def _get_material_style(self) -> str:
        """获取Material Design样式"""
        if self._theme == ThemeMode.LIGHT:
            return """
                QComboBox {
                    background-color: #ffffff;
                    border: 1px solid #e0e0e0;
                    border-radius: 4px;
                    padding: 12px 16px;
                    color: #212121;
                    font-size: 15px;
                    min-height: 24px;
                }
                
                QComboBox:hover {
                    border-color: #1976d2;
                    background-color: #fafafa;
                }
                
                QComboBox:focus {
                    border-color: #1976d2;
                    background-color: #ffffff;
                }
                
                QComboBox::drop-down {
                    subcontrol-origin: padding;
                    subcontrol-position: top right;
                    width: 32px;
                    border: none;
                    background-color: transparent;
                }
                
                QComboBox::down-arrow {
                    image: url(:/mqss/down_list.svg);
                    width: 16px;
                    height: 16px;
                    margin: 0px;
                    subcontrol-origin: content;
                    subcontrol-position: center;
                }
                
                QComboBox QAbstractItemView {
                    background-color: #ffffff;
                    border: none;
                    border-radius: 4px;
                    selection-background-color: #1976d2;
                    selection-color: #ffffff;
                    outline: none;
                    font-size: 16px;
                    padding: 6px;
                    min-height: 28px;
                }
                
                QComboBox QAbstractItemView::item {
                    padding: 12px 16px;
                    min-height: 28px;
                    border: none;
                    border-radius: 2px;
                }
            """
        else:  # DARK theme
            return """
                QComboBox {
                    background-color: #303030;
                    border: 1px solid #424242;
                    border-radius: 4px;
                    padding: 12px 16px;
                    color: #ffffff;
                    font-size: 15px;
                    min-height: 24px;
                }
                
                QComboBox:hover {
                    border-color: #42a5f5;
                    background-color: #383838;
                }
                
                QComboBox:focus {
                    border-color: #42a5f5;
                    background-color: #303030;
                }
                
                QComboBox::drop-down {
                    subcontrol-origin: padding;
                    subcontrol-position: top right;
                    width: 32px;
                    border: none;
                    background-color: transparent;
                }
                
                QComboBox::down-arrow {
                    image: url(:/mqss/down_list.svg);
                    width: 16px;
                    height: 16px;
                    margin: 0px;
                    subcontrol-origin: content;
                    subcontrol-position: center;
                }
                
                QComboBox QAbstractItemView {
                    background-color: #303030;
                    border: none;
                    border-radius: 4px;
                    selection-background-color: #42a5f5;
                    selection-color: #ffffff;
                    outline: none;
                    font-size: 16px;
                    padding: 6px;
                    min-height: 28px;
                }
                
                QComboBox QAbstractItemView::item {
                    padding: 12px 16px;
                    min-height: 28px;
                    border: none;
                    border-radius: 2px;
                }
            """


if __name__ == "__main__":
    import sys
    try:
        from ..handlercore.imagesource import qInitResources
        qInitResources()
    except ImportError:
        # 如果相对导入失败，尝试绝对导入或跳过资源初始化
        try:
            from mooetoolapp.handlercore.imagesource import qInitResources
            qInitResources()
        except ImportError:
            print("Warning: Could not load image resources")
    app = QApplication(sys.argv)
    
    # 创建测试窗口
    widget = QWidget()
    widget.setWindowTitle("Custom ComboBox Test")
    widget.resize(400, 300)
    
    from PySide2.QtWidgets import QVBoxLayout, QHBoxLayout, QLabel, QPushButton
    
    layout = QVBoxLayout(widget)
    
    # 测试不同样式的ComboBox
    styles = [
        (ComboBoxStyle.DEFAULT, "默认样式"),
        (ComboBoxStyle.MODERN, "现代样式"),
        (ComboBoxStyle.ROUNDED, "圆角样式"),
        (ComboBoxStyle.FLAT, "扁平样式"),
        (ComboBoxStyle.MATERIAL, "Material样式")
    ]
    
    combos = []
    
    for style, name in styles:
        h_layout = QHBoxLayout()
        label = QLabel(name)
        label.setMinimumWidth(100)
        
        combo = CustomComboBox(style, ThemeMode.LIGHT)
        combo.addItems(["选项1", "选项2", "选项3", "选项4"])
        
        h_layout.addWidget(label)
        h_layout.addWidget(combo)
        layout.addLayout(h_layout)
        
        combos.append(combo)
    
    # 主题切换按钮
    theme_btn = QPushButton("切换到深色主题")
    current_theme = ThemeMode.LIGHT
    
    def toggle_theme():
        global current_theme
        if current_theme == ThemeMode.LIGHT:
            current_theme = ThemeMode.DARK
            theme_btn.setText("切换到浅色主题")
            widget.setStyleSheet("background-color: #2b2b2b; color: white;")
        else:
            current_theme = ThemeMode.LIGHT
            theme_btn.setText("切换到深色主题")
            widget.setStyleSheet("background-color: white; color: black;")
        
        for combo in combos:
            combo.set_theme(current_theme)
    
    theme_btn.clicked.connect(toggle_theme)
    layout.addWidget(theme_btn)
    
    widget.show()
    sys.exit(app.exec_())